/* eslint-disable max-classes-per-file */

// ws SYS_MSG data 识别类型
// eslint-disable-next-line no-shadow
export enum V_msgType {
  BROADCAST = 0, // 广播
  EARLY_WARNING, // 预警
  ALARM, // 警报
  NOTICE, // 通知
  TO_DO_LIST, // 待办事项
  REFRESH_TOKEN, // 更新token
  WS_SERVER_INFO, // ws服务器信息
  SUBSCRIPTION // 订阅
}

export enum V_msgCommand {
  MESSAGE = 0,
  CUSTOM_COMMUNICATION,
  TALKING
}

// 基础消息
export class T_baseMsg {
  // 消息标题 支持国际化
  title: string;

  // 消息内容 支持国际化
  content: string;

  constructor() {
    this.title = '';
    this.content = '';
  }

  public setTitle(title: string): T_baseMsg {
    this.title = title;
    return this;
  }

  public getTitle(): string {
    return this.title;
  }

  public setContent(content: string): T_baseMsg {
    this.content = content;
    return this;
  }

  public getContent(): string {
    return this.content;
  }
}
// 更新token
export class T_msgRefreshToken {
  token: string;
}
// ws服务器信息
export class T_msgWsServerInfo {
  connCliID: string;
}
// 广播
export class T_msgBroadcast extends T_baseMsg {
  category: string;

  constructor(title?: string, content?: string, category?: string) {
    super();
    this.title = title;
    this.content = content;
    this.category = category;
  }

  public setCategory(category: string): T_msgBroadcast {
    this.category = category;
    return this;
  }

  public getCategory(): string {
    return this.category;
  }
}
// 预警
export class T_msgEarlyWarning extends T_baseMsg {
  category: string;

  constructor(title?: string, content?: string, category?: string) {
    super();
    this.title = title;
    this.content = content;
    this.category = category;
  }

  public setCategory(category: string): T_msgEarlyWarning {
    this.category = category;
    return this;
  }

  public getCategory(): string {
    return this.category;
  }
}
// 警报
export class T_msgAlarm extends T_baseMsg {
  category: string;

  constructor(title?: string, content?: string, category?: string) {
    super();
    this.title = title;
    this.content = content;
    this.category = category;
  }

  public setCategory(category: string): T_msgAlarm {
    this.category = category;
    return this;
  }

  public getCategory(): string {
    return this.category;
  }
}
// 通知
export class T_msgNotice extends T_baseMsg {
  category: string;

  constructor(title?: string, content?: string, category?: string) {
    super();
    this.title = title;
    this.content = content;
    this.category = category;
  }

  public setCategory(category: string): T_msgNotice {
    this.category = category;
    return this;
  }

  public getCategory(): string {
    return this.category;
  }
}
// 待办事项
export class T_msgToDoList extends T_baseMsg {
  category: string;

  // 分类
  liaisonID?: string; // 联系单ID

  // eslint-disable-next-line no-useless-constructor
  constructor(
    title?: string,
    content?: string,
    category?: string,
    liaisonID?: string
  ) {
    super();
    this.title = title;
    this.content = content;
    this.category = category;
    this.liaisonID = liaisonID;
  }

  public setCategory(category: string): T_msgToDoList {
    this.category = category;
    return this;
  }

  public getCategory(): string {
    return this.category;
  }

  public setLiaisonID(liaisonID: string): T_msgToDoList {
    this.liaisonID = liaisonID;
    return this;
  }

  public getLiaisonID(): string {
    return this.liaisonID;
  }
}
// scadaMonbos订阅
export class T_msgScadaModbusSubscription {
  appNum: number; // 应用编号

  dataType: string; // 数据类型

  value: any;
}

// scadaEcCode订阅
export class T_msgScadaEcCodeSubscription {
  name: string;

  dataType: string;

  // 数据类型
  controllerType: string;

  // 控制器类型
  timestamp: string;

  v: any;

  err: string;
}
export interface I_msgScadaEcCodeSubscription {
  [key: string]: T_msgScadaEcCodeSubscription;
}

// 订阅
export type T_msgSubscription =
  | Array<T_msgScadaModbusSubscription>
  | I_msgScadaEcCodeSubscription;

// 自定义
export class T_msgCustomCommunication {
  // 自定义消息二次路由ID .请求方有可能多个程序同一个WsCliID.
  private routeID: string;

  private content: any;

  public setRouteID(id: string): T_msgCustomCommunication {
    this.routeID = id;
    return this;
  }

  public getRouteID(): string {
    return this.routeID;
  }

  public setContent(p: any): T_msgCustomCommunication {
    this.content = p;
    return this;
  }

  public getContent(): any {
    return this.content;
  }
}
export interface I_msgCustomCommunication extends T_msgCustomCommunication {}

export class T_msgTalking {
  // 自定义消息二次路由ID .请求方有可能多个程序同一个WsCliID.
  private routeID: string;

  private content: string;

  public setRouteID(id: string): T_msgTalking {
    this.routeID = id;
    return this;
  }

  public getRouteID(): string {
    return this.routeID;
  }

  public setContent(p: any): T_msgTalking {
    this.content = p;
    return this;
  }

  public getContent(): any {
    return this.content;
  }
}
export interface I_msgTalking extends T_msgTalking {}

export type I_msgContent =
  | T_msgBroadcast
  | T_msgEarlyWarning
  | T_msgAlarm
  | T_msgNotice
  | T_msgToDoList
  | T_msgRefreshToken
  | T_msgWsServerInfo
  | T_msgSubscription
  | T_msgCustomCommunication
  | T_msgTalking;
// 消息体  ws-message-body （消息体类封装）
// ---说明---
// 广播 ：0：紧急广播、
// 预警 ：
// 警报 ：
// 通知 ：0：通知公告、
// 待办事项 ：0：单据审核、
// 更新token ：
// ws服务器信息相关
// 更新token ：消息订阅

export class T_wsMsg {
  // 发给哪个用户 广播 "__ALLUSERS__"
  protected toUserNo: string;

  // 发给对方ws客户端ID,用id发送可以解决一用户多点登录问题
  protected toClientID?: string;

  // 消息
  protected msg: {
    cmd: V_msgCommand;
    sessID?: string; // 当CMD=3(TALKING会谈模式) 此处作为应答的会话ID,在返回消息中对应取信息
    m: {
      t: V_msgType;
      d: I_msgContent;
    };
  };

  constructor() {
    this.msg = {
      cmd: null,
      m: {
        t: null,
        d: null
      }
    };
  }

  public setToUserNo(p: string): T_wsMsg {
    this.toUserNo = p;
    return this;
  }

  // 当CMD=3(TALKING会谈模式) 此处作为应答的会话ID
  public setSessionID(p: string): T_wsMsg {
    this.msg.sessID = p;
    return this;
  }

  public setToClientID(p: string): T_wsMsg {
    this.toClientID = p;
    return this;
  }

  public setData(p: any): T_wsMsg {
    this.msg.m.d = p;
    return this;
  }

  /**
   * @description: V_msgType 消息类型
      BROADCAST = 0, // 广播
      EARLY_WARNING, // 预警
      ALARM, // 警报
      NOTICE, // 通知
      TO_DO_LIST, // 待办事项
      REFRESH_TOKEN, // 更新token
      WS_SERVER_INFO, // ws服务器信息
      SUBSCRIPTION // 订阅
   * @param {V_msgType} p
   * @return {*}
   * @author: swc
   * @Date: 2024-12-06 15:11:19
   * @LastEditors: swc
   * @LastEditTime: Do not edit
   */
  public setMsgType(p: V_msgType): T_wsMsg {
    this.msg.m.t = p;
    return this;
  }

  public setCMD(p: V_msgCommand): T_wsMsg {
    this.msg.cmd = p;
    return this;
  }

  public showJsonMsg(): string {
    return JSON.stringify({
      toUserNo: this.toUserNo,
      toClientID: this.toClientID,
      msg: this.msg
    });
  }
}
export interface I_wsUserMsg extends T_wsMsg {}

// 发送ws消息
export class T_sendWsMsgParams {
  protected userMsgs: Array<I_wsUserMsg>;

  constructor() {
    this.userMsgs = [];
  }

  public addUserMsgs(...p: Array<I_wsUserMsg>) {
    this.userMsgs.push(...p);
  }
}
export interface I_sendWsMsgParams extends T_sendWsMsgParams {}
// 接收ws消息
export class T_receWsMsgParams {
  cmd: V_msgCommand;

  sessID?: string; // 当CMD=3(TALKING会谈模式) 此处作为应答的回信对应

  // 消息体
  m: {
    t: V_msgType; // 消息类型
    uno: string; // fromUserNo
    cid: string; // fromClientID
    d: I_msgContent; // 消息内容
  };
}
export interface I_receWsMsgParams extends T_receWsMsgParams {}
